﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExportToPLMX.ViewModels.CUiETPXContentViewModel
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.ExportToPLMX.Resources;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ExportToPLMX.ViewModels;

public class CUiETPXContentViewModel : ObservableObject
{
  private readonly string LEVEL_OF_DETAILS = "LevelOfDetails";
  private ITxOperation _currOperation;
  private bool? _exportSimulation;
  private bool _isOperationValid;
  private int _SelectedLOD;
  private List<string> m_lodItemsList;

  public ITxOperation CurrOperation
  {
    get => this._currOperation;
    set
    {
      if (value != null)
        this._isOperationValid = true;
      this.Set<ITxOperation>(ref this._currOperation, value, nameof (CurrOperation));
    }
  }

  public bool? ExportSimulation
  {
    get => this._exportSimulation;
    set
    {
      if (!value.Value)
      {
        this.CurrOperation = (ITxOperation) null;
        this.IsOperationValid = false;
      }
      this.Set<bool?>(ref this._exportSimulation, value, nameof (ExportSimulation));
    }
  }

  public bool IsOperationValid
  {
    get
    {
      if (!this.ExportSimulation.Value)
        return true;
      return this.CurrOperation != null && this._isOperationValid;
    }
    set => this._isOperationValid = value;
  }

  public CUiETPXContentViewModel()
  {
    this.InitOperationControl();
    this.CreateLodItemsList();
    this.LoadLODFromRegistry();
  }

  private void InitOperationControl()
  {
    this.IsOperationValid = true;
    ITxOperation currPickedOperation = this.GetCurrPickedOperation();
    if (currPickedOperation != null)
    {
      this.ExportSimulation = new bool?(true);
      this.CurrOperation = currPickedOperation;
    }
    else
      this.ExportSimulation = new bool?(false);
  }

  public int SelectedLOD
  {
    get => this._SelectedLOD;
    set => this.Set<int>(ref this._SelectedLOD, value, nameof (SelectedLOD));
  }

  private ITxOperation GetCurrPickedOperation()
  {
    TxObjectList filteredItems = TxApplication.ActiveSelection.GetFilteredItems((ITxTypeFilter) new TxTypeFilter(typeof (ITxOperation)));
    return ((Collection<ITxObject>) filteredItems).Count > 0 ? ((Collection<ITxObject>) filteredItems)[0] as ITxOperation : (ITxOperation) null;
  }

  public void PersistData()
  {
    CCoExportToPLMXUtils.SaveValueToRegistry(this.LEVEL_OF_DETAILS, this.SelectedLOD.ToString());
  }

  private void LoadLODFromRegistry()
  {
    int result;
    if (int.TryParse(CCoExportToPLMXUtils.ReadValueFromRegistry(this.LEVEL_OF_DETAILS), out result))
      this.SelectedLOD = result;
    else
      this.SelectedLOD = 0;
  }

  public List<string> LodItemsList
  {
    get => this.m_lodItemsList;
    set => this.Set<List<string>>(ref this.m_lodItemsList, value, nameof (LodItemsList));
  }

  private void CreateLodItemsList()
  {
    this.LodItemsList = new List<string>();
    this.LodItemsList.Add(CApETPXStringTable.ETPX_LOD_HIGH);
    this.LodItemsList.Add(CApETPXStringTable.ETPX_LOD_MED);
    this.LodItemsList.Add(CApETPXStringTable.ETPX_LOD_LOW);
  }
}
